package com.yidu.hbatsft.action;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.*;
import com.yidu.privilege.Utils.BaseMessage;
import com.yidu.utils.FileTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/atsWeek")
public class AtsPlanWeekController {

    @Resource
    AtsPlanWeekService atsPlanWeekService;//周日计划

    @Resource
    AtsBaseSportService atsBaseSportService;// 体育项目管理

    @Resource
    AtsEmpCoachService atsEmpCoachService;// 教练员

    @Resource
    AtsPlanItemService atsPlanItemService;//指标

    @Resource
    AtsPlanPhaseService atsPlanPhaseService;//阶段计划

    @Resource
    FileTools fileTools;//文件上传

    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, AtsPlandimSearch atsPlandimSearch){
        Map<String, Object> map = new HashMap<String, Object>();
        List<AtsPlanWeek> list = atsPlanWeekService.selectAll(pageNum,pageSize,atsPlandimSearch);//分页数据
        Integer count = atsPlanWeekService.count(atsPlandimSearch);
        map.put("code", 0);
        map.put("msg", "");
        map.put("msg", "");
        map.put("count", count);
        map.put("data",list);

        return map;
    }

    @RequestMapping("selMessage")
    @ResponseBody
    public Map<String,Object> selMessage(){

        Map<String, Object> map = new HashMap<String, Object>();
        //所有运动项目名
        List<AtsBaseSport> atsBaseSport = atsBaseSportService.selAllSportNmae();
        //所有教练员的姓名
        List<AtsEmpCoach> atsEmpCoach = atsEmpCoachService.selAllCoachName();
        //指标
        List<AtsPlanItem> atsPlanItem = atsPlanItemService.selectItem("4");
        //阶段计划
        List<AtsPlanPhase> atsPlanPhases = atsPlanPhaseService.selectPhaseName();

        map.put("sport",atsBaseSport);
        map.put("coach",atsEmpCoach);
        map.put("item",atsPlanItem);
        map.put("phase",atsPlanPhases);

        ObjectMapper mapper = new ObjectMapper();
        String rs = null;
        try {
            rs = mapper.writeValueAsString(map);
            System.out.println(rs);
        } catch (Exception e) {
            System.out.println(123);
        }

        return map;
    }

    @RequestMapping("addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(AtsPlanWeek atsPlanWeek, AtsPlanItem atsPlanItem, String itemIdArry, MultipartFile file1s, MultipartFile file2s, MultipartFile file3s){

        BaseMessage bm = new BaseMessage();

        atsPlanWeek.setCreatTime(new Date());//设置创建时间
        atsPlanWeek.setCreater("sb");//设置当前登陆人

        if (!"".equals(file1s.getOriginalFilename()) && file1s.getOriginalFilename() != null){
            try {
                String file1 = fileTools.saveFile("images",file1s);
                atsPlanWeek.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanWeek.setFile1(null);
        }
        if (!"".equals(file2s.getOriginalFilename()) && file2s.getOriginalFilename() != null){
            try {
                String file2 = fileTools.saveFile("images",file2s);
                atsPlanWeek.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanWeek.setFile2(null);
        }
        if (!"".equals(file3s.getOriginalFilename()) && file3s.getOriginalFilename() != null){
            try {
                String file3 = fileTools.saveFile("images",file3s);
                atsPlanWeek.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanWeek.setFile3(null);
        }

        atsPlanItemService.itemUpdte("4");//将上一次指标都改为0

        AtsBaseSport atsBaseSport = atsBaseSportService.selSportNmaeLX(atsPlanWeek.getSportId());//查询出atsPlanYear的item名
        atsPlanWeek.setItemName(atsBaseSport.getSportName());

        Integer temp = null;
        if(atsPlanWeek.getWeekId() != 0){
            temp = atsPlanWeekService.updateByPrimaryKeySelective(atsPlanWeek,atsPlanItem,itemIdArry);

        }else{
            temp = atsPlanWeekService.insertSelective(atsPlanWeek,atsPlanItem);
        }
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("操作成功");
        }else {
            bm.setState(0);
            bm.setMsg("操作失败");
        }
        return bm;
    }

    @RequestMapping("updSel")
    @ResponseBody
    public Map<String,Object> selectByPrimaryKey(Integer pid){
        AtsPlanWeek atsPlanWeek = null;
        List<AtsPlanItem> atsPlanItem = null;
        Map<String, Object> map = null;
        if (pid != null && !"".equals(pid)){
            map = new HashMap<String, Object>();
            atsPlanWeek = atsPlanWeekService.selectByPrimaryKey(pid);
            atsPlanItem = atsPlanItemService.selectItemByPid(pid.toString(),"4");

            List<String> list = null;
            if(atsPlanWeek.getOtherCoach() != null && !"".equals(atsPlanWeek.getOtherCoach())) {
                String otherNameTemp[] = atsPlanWeek.getOtherCoach().split(",");
                list = new ArrayList<String>();
                if (otherNameTemp[0] != null && !"".equals(otherNameTemp[0])) {
                    for (int i = 0; i < otherNameTemp.length; i++) {
                        String otherName = otherNameTemp[i];
                        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selCoachId(otherName);
                        list.add(atsEmpCoach.getCoachId().toString());
                    }
                } else {
                    list.add(null);
                }
            }
            map.put("YearMore",atsPlanWeek);
            map.put("Item",atsPlanItem);
            map.put("otherNameId",list);
        }

        return map;
    }

    @RequestMapping("Delete")
    @ResponseBody
    public BaseMessage updateDelete(String ids){
        BaseMessage bm = new BaseMessage();
        Integer temp = atsPlanWeekService.updateDelete(ids);
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("删除成功");
        }else {
            bm.setState(0);
            bm.setMsg("删除失败");
        }
        return bm;
    }

}
